# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-base
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_qtver=6.9.0
pkgver=${_qtver/-/}
pkgrel=2
pkgdesc="A cross-platform application and UI framework (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io'
msys2_repository_url="https://code.qt.io/cgit/qt/qtbase.git"
msys2_references=(
  'archlinux: qt6-base'
  "cpe: cpe:/a:qt:qt"
)
license=('spdx:LGPL-3.0-only WITH Qt-GPL-exception-1.0 AND AFL-2.1 AND Apache-2.0 AND BSL-1.0 AND CC0-1.0 AND BSD-3-Clause AND CC-BY-4.0 AND GFDL-1.3-no-invariants-only AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-only AND custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-double-conversion"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-harfbuzz"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-libb2"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-md4c"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-sqlite3"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             "${MINGW_PACKAGE_PREFIX}-xmlstarlet"
             "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-firebird")
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "rsync")
_pkgfn="${_realname/6-/}-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfn}.tar.xz"
        001-appending-qt6-to-remove-qt5-conflict.patch
        002-fix-qtdocs-helpers-cmake.patch
        003-adjust-qmake-conf-mingw.patch
        004-qt-6.2.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        005-qt-6.7.0-opengl-header.patch
        006-qt-6.2.0-dont-add-resource-files-to-qmake-libs.patch
        007-Fix-crashes-in-rasterization-code-using-setjmp.patch
        009-qfileinfo-undefine-mingw-stat.patch
        010-export-some-constexpr-variables.patch
        011-qt6-windeployqt-fixes.patch)
sha256sums=('c1800c2ea835801af04a05d4a32321d79a93954ee3ae2172bbeacf13d1f0598c'
            'c9f61d90ba7bce89f935c3e8ea90c448a641fd206666ef2d1542f61a3a655fc9'
            '0dac4b7c07ed52367f55cb54df5aee6966601edc579861e2504701f04c509b9b'
            '68156b8b7717a0ce19c4b991942469171bfa048cd5c90765115a546e65669a1d'
            'ed5b61bcb367bbda459bec903d796ea45604278f577a988d602ade07ec6bf363'
            'a2afc74d181864409dc96eca368b647c0f79e25751db88e3263f2d1101edf8e4'
            '4085a10b290b8e3d930de535cbad2ba3e643432cba433aa2b28fe664f86d38a3'
            '3a256533401a48aff7e3c4b02118d62a0cccc2b3566c6e550e7b467aca3e496f'
            'f4261d43a142a24e5fa3b23e25813754839db84078cc8c6dc611139bf531e64a'
            '23656a7839d7dcb763d022722d723493c847914b0639bab861ddb05d823af5b7'
            '742a15191e618a50c1fb4b93e87dda73a2bd130a6cb829cb2026256ec2c252e4')

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd $srcdir/${_pkgfn}

  apply_patch_with_msg \
    001-appending-qt6-to-remove-qt5-conflict.patch \
    002-fix-qtdocs-helpers-cmake.patch \
    003-adjust-qmake-conf-mingw.patch \
    004-qt-6.2.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    005-qt-6.7.0-opengl-header.patch \
    006-qt-6.2.0-dont-add-resource-files-to-qmake-libs.patch \
    007-Fix-crashes-in-rasterization-code-using-setjmp.patch \
    009-qfileinfo-undefine-mingw-stat.patch \
    011-qt6-windeployqt-fixes.patch

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    apply_patch_with_msg \
      010-export-some-constexpr-variables.patch
  fi

  local _ARCH_TUNE=
  if [[ ${CARCH} == x86_64 ]]; then
    _ARCH_TUNE="-march=nocona -msahf -mtune=generic"
  fi

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_CXXFLAGS .*= \(.*\)$|QMAKE_CXXFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" mkspecs/${_platform}/qmake.conf
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    --log-level=STATUS \
    -GNinja \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DQT_QMAKE_TARGET_MKSPEC=${_platform} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DINSTALL_INCLUDEDIR=include/qt6 \
    -DINSTALL_ARCHDATADIR=share/qt6 \
    -DINSTALL_DATADIR=share/qt6 \
    -DINSTALL_MKSPECSDIR=share/qt6/mkspecs \
    -DINSTALL_DESCRIPTIONSDIR=share/qt6/modules \
    -DFEATURE_system_doubleconversion=ON \
    -DFEATURE_system_freetype=ON \
    -DFEATURE_system_jpeg=ON \
    -DFEATURE_system_harfbuzz=ON \
    -DFEATURE_system_pcre2=ON \
    -DFEATURE_system_png=ON \
    -DFEATURE_system_sqlite=ON \
    -DFEATURE_system_zlib=ON \
    -DFEATURE_zstd=ON \
    -DFEATURE_openssl=ON \
    -DFEATURE_opengl=ON \
    -DFEATURE_opengl_desktop=ON \
    -DFEATURE_dbus=ON \
    -DFEATURE_glib=ON \
    -DFEATURE_icu=ON \
    -DFEATURE_pkg_config=ON \
    -DFEATURE_vulkan=ON \
    -DFEATURE_sql_sqlite=ON \
    -DFEATURE_sql_psql=ON \
    -DFEATURE_sql_mysql=ON \
    -DFEATURE_sql_odbc=ON \
    -DFEATURE_sql_ibase=$([[ ${CARCH} == aarch64 ]] && echo "OFF" || echo "ON")  \
    -DFEATURE_gtk3=OFF \
    -DFEATURE_regularexpression=ON \
    -DFEATURE_force_debug_info=ON \
    -DFEATURE_separate_debug_info=ON \
    ../${_pkgfn}

  PATH=$PWD/bin:$PATH ${MINGW_PREFIX}/bin/cmake --build .
}

package_qt6-base() {
  depends=("${MINGW_PACKAGE_PREFIX}-double-conversion"
           "${MINGW_PACKAGE_PREFIX}-dbus"
           "${MINGW_PACKAGE_PREFIX}-freetype"
           "${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-harfbuzz"
           "${MINGW_PACKAGE_PREFIX}-icu"
           "${MINGW_PACKAGE_PREFIX}-libb2"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
           "${MINGW_PACKAGE_PREFIX}-libpng"
           "${MINGW_PACKAGE_PREFIX}-md4c"
           "${MINGW_PACKAGE_PREFIX}-openssl"
           "${MINGW_PACKAGE_PREFIX}-pcre2"
           "${MINGW_PACKAGE_PREFIX}-sqlite3"
           "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
           "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  optdepends=("${MINGW_PACKAGE_PREFIX}-libmariadbclient: MySQL/MariaDB driver"
              "${MINGW_PACKAGE_PREFIX}-postgresql: PostgreSQL driver")
  if [[ ${CARCH} != aarch64 ]]; then
    optdepends+=("${MINGW_PACKAGE_PREFIX}-firebird: Firebird/iBase driver")
  fi
  groups=("${MINGW_PACKAGE_PREFIX}-qt6")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $_pkgfn/LICENSES/* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}

  # Seperate debug-info files
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs "${pkgdir}"/.${MINGW_PREFIX} "${srcdir}"/${MSYSTEM}-debug/

  # Restore qmake6, qtpaths6 and windeployqt6
  ln -s "${pkgdir}"${MINGW_PREFIX}/bin/qmake-qt6 "${pkgdir}"${MINGW_PREFIX}/bin/qmake6.exe
  ln -s "${pkgdir}"${MINGW_PREFIX}/bin/qtpaths-qt6 "${pkgdir}"${MINGW_PREFIX}/bin/qtpaths6.exe
  ln -s "${pkgdir}"${MINGW_PREFIX}/bin/windeployqt-qt6 "${pkgdir}"${MINGW_PREFIX}/bin/windeployqt6.exe

  # Fix *.pri, *.bat and *.cmake files
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/share/qt6" -type f \( -name '*.pri' -o -name '*.bat' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/bin" -type f \( -name '*.bat' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

package_qt6-base-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6-debug")
  options=('!strip')

  cp -rf "${srcdir}"/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
